﻿package 
{
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;

	public class GravityTest1 extends MovieClip
	{

		private var lvlHandler:LevelHandler;



		private var grav:Number = 0;
		private var gacc:Number = 0.5;
		private var started:Boolean = false;

		private var level:Sprite;
		private var ground:Ground;
		private var man:Man;
		private var wall:Sprite;


		//keys down checks
		private var up:Boolean = false;
		private var down:Boolean = false;
		private var right:Boolean = false;
		private var left:Boolean = false;

		public function GravityTest1()
		{
			init();
		}
		private function init()
		{

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;

			lvlHandler = new LevelHandler();

			ground = new Ground();
			ground.width = stage.stageWidth;
			addChild(ground);
			ground.x = stage.stageWidth/2;
			ground.y = stage.stageHeight-ground.height/2;


			level = new Sprite();
			addChild(level);

			man = new Man(-6);
			//addChild(man);
			//man.gotoAndStop(1);
			/*man.x = stage.stageWidth/2;
			man.y = ground.y;*/

			createLvl();

			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, myKeyUp);
		}
		public function onEnterFrame(event:Event)
		{
			if (started) {
				if (man.Hit.hitTestObject(ground))
				{
					man.falling = false;
					grav = 0;
					man.y = ground.y;
					if (!man.running)
					{
						if (man.currentFrame != 4) {
							man.gotoAndStop(1);
						}
					}
				}
				else
				{
					man.falling = true;
				}
				for (var i=0; i<level.numChildren; i++)
				{
					var w = level.getChildAt(i);
	
					//if (man.hitTestObject(w)) {
					if (man.Hit.hitTestObject(w.Left))
					{
						if (w is Goal) {

						}
						level.x += man.xspeed;
					}
					if (man.Hit.hitTestObject(w.Right))
					{
						if (w is Goal) {

						}
						level.x -= man.xspeed;
					}
					if (man.Hit.hitTestObject(w.Top))
					{
						if (w is Goal) {

						}
						man.falling = false;
						grav = 0;
						man.y = w.y;
						if (!man.running)
						{
							if (man.currentFrame != 4) {
								man.gotoAndStop(1);
							}
						}
					}
					if (man.Hit.hitTestObject(w.Bottom))
					{
						if (w is Goal) {

						}
						man.falling = true;
						man.y = w.y+w.height+man.height;
						grav = 0;
					}
					
					//}
	
					/*if ((man.x+man.width/2)>=(w.x+level.x) && 
					(man.x-man.width/2)<=(w.x+w.width+level.x) && 
					(man.y > w.y) &&
					(man.y-man.height < w.y+w.height)) {
					if ((w.x+level.x-man.x) < 0) {
					level.x -= man.xspeed;
					} else {
					level.x += man.xspeed;
					}
					}*/
				}
				
				if (man.jumping && man.running && !man.falling)
				{
					man.gotoAndStop(2);
					man.jumping = false;
				}
				if (!man.falling && up)
				{
					grav = man.jumpspeed;
					man.falling = true;
					man.gotoAndStop(3);
					man.jumping = true;
				}
				if (man.falling)
				{
					man.y += grav;
					grav += gacc;
				}
				if (left)
				{
					level.x += man.xspeed;
					man.scaleX = -1;
				}
				if (right)
				{
					level.x -= man.xspeed;
					man.scaleX = 1;
				}
			}
		}
		private function myKeyDown(event:KeyboardEvent):void
		{

			if (event.keyCode == Keyboard.UP && !down)
			{
				up = true;
			}
			if (event.keyCode == Keyboard.RIGHT && !left)
			{
				right = true;
				if (!man.running && !man.falling && started)
				{
					man.gotoAndStop(2);
					man.running = true;
				}
			}
			if (event.keyCode == Keyboard.LEFT && !right)
			{
				left = true;
				if (!man.running && !man.falling && started)
				{
					man.gotoAndStop(2);
					man.running = true;
				}
			}
			if (event.keyCode == Keyboard.DOWN && !up)
			{
				down = true;
			}
			if (event.keyCode == Keyboard.SPACE)
			{
				started = !started;
			}
		}
		private function myKeyUp(event:KeyboardEvent):void
		{

			if (event.keyCode == Keyboard.UP)
			{
				up = false;
			}
			if (event.keyCode == Keyboard.RIGHT)
			{
				right = false;
			}
			if (event.keyCode == Keyboard.LEFT)
			{
				left = false;
			}
			if (event.keyCode == Keyboard.DOWN)
			{
				down = false;
			}
			if (man.running && !left && !right)
			{
				man.running = false;
				if (!man.falling)
				{
					man.gotoAndStop(1);
				}
			}
		}
		function createLvl():void
		{
			var lvlArray:Array = lvlHandler.getCurrentLevel();
			for (var i:int = 0; i<lvlArray.length; i++)
			{
				for (var j:int = 0; j<lvlArray[i].length; j++)
				{
					var newWall:Wall = new Wall();
					if (lvlArray[i][j] == 1)
					{
						newWall.x = j*newWall.width;
						newWall.y = i*newWall.height;
						level.addChild(newWall);
					}
					else if (lvlArray[i][j] == "MAIN")
					{
						man.x = j*newWall.width+newWall.width/2;
						man.y = i*newWall.height+newWall.height;
						addChild(man);
						man.gotoAndStop(4);
					}
					else if (lvlArray[i][j] == "GOAL")
					{
						var goal:Goal = new Goal();
						goal.x = j*newWall.width;
						goal.y = i*newWall.height;
						level.addChild(goal);
					}
				}
			}
			var manMove:int = stage.stageWidth/2 - man.x;
			man.x += manMove;
			level.x += manMove;

		}
	}
}